package de.schoar.nagroid.notification;

import java.util.logging.Level;
import java.util.logging.Logger;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Vibrator;
import de.Ox539.nagroid.SuppressHelper;
import de.schoar.nagroid.ConfigurationAccess;
import de.schoar.nagroid.DM;
import de.schoar.nagroid.HealthState;
import de.schoar.nagroid.PIFactory;
import de.schoar.nagroid.R;
import de.schoar.nagroid.activity.ProblemsActivity;
import de.schoar.nagroid.nagios.NagiosState;

public class HealthNotificationHelper extends NotificationHelper {
	private ConfigurationAccess mConfigurationAccess;
	private HealthState mLastHealthState = null;
	private boolean mIsDisplayed = false;
	private SuppressHelper mSuppressHelper = new SuppressHelper();

	public HealthNotificationHelper(final Context ctx, final ConfigurationAccess ca) {
		super(ctx, R.layout.problems);
		mConfigurationAccess = ca;
	}

	public void updateNagiosState(final Context ctx, final NagiosState stateHost,
			final NagiosState stateService, boolean noErrorOccured) {
		Logger.getAnonymousLogger().log(Level.SEVERE, "public updateNagiosState called");
		updateNagiosState(ctx, new HealthState(noErrorOccured, stateHost,
				stateService), false);
	}

	public void showLast(final Context ctx) {
		if (mLastHealthState == null) {
			return;
		}
		updateNagiosState(ctx, mLastHealthState, true);
	}

	@Override
	public void clear() {
		super.clear();
		mIsDisplayed = false;
	}

	private boolean checkAdditionalQuiets(final Context ctx, final HealthState hs) {
		if(DM.I.getConfiguration().inTimeSwitchDisabledRange())
			return true;
		
		if(mLastHealthState == null)
			// First poll in the application's lifetime.  Don't ignore.
			return false;
		
		final int suppressLimit = DM.I.getConfiguration().getNotificationConsecutiveFailedPolls();
		
		if(mSuppressHelper.shouldSuppress(mLastHealthState.isOk(), hs.isOk(), suppressLimit)) {
			// Alarm suppressed.
			return true;
		}
		
		return false;
	}
	
	private void updateNagiosState(final Context ctx, final HealthState hs, boolean quiet) {
		if(!quiet)
			quiet = checkAdditionalQuiets(ctx, hs);
		
		if (!quiet) {
			vibrate(ctx, hs);
			playSound(ctx, hs);
		}

		if (mLastHealthState != null && mLastHealthState.equals(hs)
				&& mIsDisplayed) {
			return;
		}

		if (mConfigurationAccess.getNotificationHideIfOk() && hs.isOk()) {
			clear();
			return;
		}

		if (!DM.I.getConfiguration().getPollingEnabled()) {
			return;
		}

		show(ctx, hs);
	}

	private void playSound(final Context ctx, final HealthState hs) {
		if (DM.I.getConfiguration().getNotificationAlarmEnabled()
				&& hs.getSoundUri() != null) {
			final MediaPlayer mp = MediaPlayer.create(ctx, hs.getSoundUri());
			mp.setOnCompletionListener(new OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer mp) {
					mp.release();
				}
			});
			mp.start();
		}
	}

	private void vibrate(final Context ctx, final HealthState hs) {
		if (hs.getVibrate().length != 0) {
			final Vibrator v = (Vibrator) ctx
					.getSystemService(Context.VIBRATOR_SERVICE);
			v.vibrate(hs.getVibrate(), -1);
		}
	}

	private void show(final Context ctx, final HealthState hs) {
		mIsDisplayed = true;
		mLastHealthState = hs;

		PendingIntent pi = PIFactory
				.getForActivity(ctx, ProblemsActivity.class);

		notify(ctx, hs.getText(), hs.getResourceId(),
				Notification.FLAG_NO_CLEAR, pi);
	}
}
